In this demonstration,
you will learn how to work with the RabbitMQ
message broker and implement a producer and a consumer
that will send and receive simple messages to
one another asynchronously. Before you get started, ensure that Docker is
installed on your machine. Steps to send and receive messages using
Node.js applications on RabbitMQ are start
the RabbitMQ management, create a producer,
create a consumer. Start by starting the
RabbitMQ management by running the Docker command in the command prompt
or PowerShell and also ensure that you have
administrator access. In order to run the Docker
container of RabbitMQ, you will use the Docker command. You will be running
it on the port 15672. Now you have specified
the port on which the Docker container
of RabbitMQ must run. Start the container. In case you are using Windows, ensure to start
the Docker desktop before you run the command. As you can see, the
RabbitMQ has started. Now open the browser and go to the URL where RabbitMQ
management is running. Now the RabbitMQ
management is running and RabbitMQ is asking for the
user ID and the password. Enter the ID as guest, which is the default username, and guest, which is
the default password, and click on "Login". Now the RabbitMQ instance
runs on port 15672. Now create your node programs. The folder is created
under which you will have the producer
and the consumer, the two programs which
are necessary to send and receive
messages on RabbitMQ. In order to do that, you will need the
amqplib npm package. You will install the
packages by using npm i amqplib command. This amqplib package contains all the necessary APIs to interact with the
RabbitMQ instance. Now create a producer.js file, which will be the
message publisher. The publisher will
connect to RabbitMQ and send a single simple
message and then exit. Start by importing the
amqp dependencies. The callback API provides all
the necessary methods and functions to perform the
operations on RabbitMQ. The next step is to
create a connection with a running RabbitMQ
management instance by using the connect method. The connect method
takes two parameters. The URL where the instance is
running, this is localhost. If the correct URL is hit, then the callback is executed. If error, then throw the error. If there is no error, the connection is established and you create a channel to send information to the queue
that is present on RabbitMQ. The channel is created using
the connection object. The createChannel function
is used to create a channel that is used to send the information
to the queue. The createChannel function takes a callback as a parameter. If error, then you
throw the error. Now, the next step is to declare a queue to which you
want to send messages. You can name the
queue as testqueue. Before any messages sent, you need to check
if the queue is present or not using
the assertQueue method. In this case, there is no queue available since the queue
is declared in the program. If the queue with the
given name is not present, a new queue with the name
will be created on RabbitMQ. The next step is to declare the message that you want
to send to the testqueue, then you will send hello
message to the queue through the channel created and the message content
is a byte array. Through the channel, invoke
the sendToQueue function, which will take the
name of the queue and the message as parameters. The message content
is a byte array, so you will convert the
string message hello to byte array using the
Buffer.from function. You can also print
the message that is being sent to the
queue on the console. Now close the
connection and exit. The close and exit of the
connection will be done in a setTimeout function after
a delay of 500 milliseconds. This way, you have built
a producer to send messages in this
demo. Thank you.